package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpBp2Mapper;
import com.ruoyi.system.domain.SpBp2;
import com.ruoyi.system.service.ISpBp2Service;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 编排2Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-12
 */
@Service
public class SpBp2ServiceImpl implements ISpBp2Service 
{
    @Autowired
    private SpBp2Mapper spBp2Mapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询编排2
     * 
     * @param cj2Id 编排2主键
     * @return 编排2
     */
    @Override
    public SpBp2 selectSpBp2ByCj2Id(Long cj2Id)
    {
        return spBp2Mapper.selectSpBp2ByCj2Id(cj2Id);
    }

    /**
     * 查询编排2列表
     * 
     * @param spBp2 编排2
     * @return 编排2
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpBp2> selectSpBp2List(SpBp2 spBp2)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spBp2.setDatascope(datascope);
        if(datascope==4){
            spBp2.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spBp2.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spBp2.setCheckHuiyuanId(0l);
            }

        }
        return spBp2Mapper.selectSpBp2List(spBp2);
    }

    @Override
    public List<SpBp2> selectSpBp2ListWeb(SpBp2 spBp2)
    {
        return spBp2Mapper.selectSpBp2List(spBp2);
    }

    /**
     * 新增编排2
     * 
     * @param spBp2 编排2
     * @return 结果
     */
    @Override
    public int insertSpBp2(SpBp2 spBp2)
    {
        return spBp2Mapper.insertSpBp2(spBp2);
    }

    /**
     * 修改编排2
     * 
     * @param spBp2 编排2
     * @return 结果
     */
    @Override
    public int updateSpBp2(SpBp2 spBp2)
    {
        return spBp2Mapper.updateSpBp2(spBp2);
    }

    /**
     * 批量删除编排2
     * 
     * @param cj2Ids 需要删除的编排2主键
     * @return 结果
     */
    @Override
    public int deleteSpBp2ByCj2Ids(Long[] cj2Ids)
    {
        return spBp2Mapper.deleteSpBp2ByCj2Ids(cj2Ids);
    }

    /**
     * 删除编排2信息
     * 
     * @param cj2Id 编排2主键
     * @return 结果
     */
    @Override
    public int deleteSpBp2ByCj2Id(Long cj2Id)
    {
        return spBp2Mapper.deleteSpBp2ByCj2Id(cj2Id);
    }
}
